package com.niit.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.niit.utils.JDBCUtils;


public class StatisticsDao {
	
	
	public static void main(String[] args) throws SQLException {
		Map<String, Double> typeSum = new StatisticsDao().getTypeSum();
		Set<Entry<String, Double>> entrySet = typeSum.entrySet();
		for (Entry<String, Double> entry : entrySet) {
			System.out.println(entry.getKey()+"--"+entry.getValue());
		}
	}
	public  Map<String, Double> getTypeSum() throws SQLException{
//		获得数据库链接
		Connection connection = JDBCUtils.getConnection();
		StringBuilder sql = new StringBuilder("SELECT product_type,sum(price) price_sum FROM product GROUP BY product_type");
		PreparedStatement ps = connection.prepareStatement(sql.toString());
		ResultSet rs = ps.executeQuery();
		Map<String, Double> map= new HashMap<>();
		while (rs.next()) {
			map.put(rs.getString("product_type"), rs.getDouble("price_sum"));
		}
		return map;
		
	}

}
